package cn.demomaster.quickrouter.actionbar;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.fragment.app.Fragment;

import com.demomaster.quickcompat.widget.ImageTextView;

import cn.demomaster.quickrouter.OnReleaseListener;
import cn.demomaster.quickrouter.R;
import cn.demomaster.quickrouter.activity.QuickActivity;
import cn.demomaster.quickrouter.fragment.QuickPage;
import cn.demomaster.quickrouter.view.QuickPageDragFrameLayout;

public class ActionBarTool implements OnReleaseListener {
    public static String TAG = QuickActivity.class.getName();

    Activity activity = null;
    Fragment fragment = null;
    Context context;
    public Context getContext() {
        if(context==null){
            if(fragment!=null){
                context = fragment.getContext();
            }
        }
        return context;
    }

    public ActionBarTool(Activity activity) {
        this.activity = activity;
        this.context = activity;
    }

    public ActionBarTool(Fragment fragment) {
        this.fragment = fragment;
    }

    //View mContentView;
    ActionBarLayout2 actionBarLayout;

    public ActionBarLayout2 getActionBarLayout() {
        return actionBarLayout;
    }

    /**
     * 设置导航栏样式
     *
     * @param actionbarType
     */
    public void setActionBarType(ACTIONBAR_TYPE actionbarType) {
        if (actionBarLayout != null) {
            actionBarLayout.setActionBarType(actionbarType);
        }
    }

    public void setActionBarPaddingTop(PaddingWith actionBarPaddingTop) {
        if (actionBarLayout != null) {
            actionBarLayout.setContentViewPaddingTop(actionBarPaddingTop);
        }
    }

    /**
     * 设置是否合并导航栏和状态栏
     *
     * @param mixStatusActionBar
     */
    public void setMixStatusActionBar(boolean mixStatusActionBar) {
        //状态栏和导航栏融合
        if (actionBarLayout != null) {
            actionBarLayout.setMixStatusActionBar(mixStatusActionBar);
        }
    }

    public void setBackgroundTransparent(){
        // 设置透明主题样式
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        } else {
            activity.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        }
    }

    // ACTIONBAR_TYPE actionbarType = NORMAL;

    //private boolean hasStatusBar = true;
    //private boolean hasActionBar = true;
   /* public void setHasStatusBar(boolean hasStatusBar) {
        builder.setHasStatusBar(hasStatusBar);
        if (actionBarLayout != null) {
            actionBarLayout.setHasStatusBar(hasStatusBar);
        }
    }*/

    /* public void setHasActionBar(boolean hasActionBar) {
         builder.setHasActionBar(hasActionBar);
         if (actionBarLayout != null) {
             actionBarLayout.setHasActionBar(hasActionBar);
         }
     }*/



   /* public View inflateView() {
        actionBarLayout = builder
                .setContentViewPaddingTop(actionBarPaddingTop)
                .setMixStatusActionBar(mixStatusActionBar)
                .creat();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            actionBarLayout.setId(View.generateViewId());
        }
        return actionBarLayout;
    }*/

    boolean isLightModle = false;//亮色

    /**
     * 设置颜色模式
     *
     * @param lightModle
     */
    public void setLightModle(boolean lightModle) {
        isLightModle = lightModle;
        resetTextColor(actionBarLayout);
    }

    public int textLightColor = Color.WHITE;
    public int textDarkColor = Color.BLACK;

    public void setActionBarThemeColors(int lightColor, int darkColor) {
        textLightColor = lightColor;
        textDarkColor = darkColor;
        resetTextColor(actionBarLayout.getActionBarView());
    }

    public int getTextColor() {
        return isLightModle ? textLightColor : textDarkColor;
    }

    /**
     * 重置文本顔色
     *
     * @param view
     */
    public void resetTextColor(View view) {
        if (view == null) {
            return;
        }
        int color = getTextColor();
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                if (viewGroup.getChildAt(i) instanceof ImageView) {
                    Drawable drawable = (((ImageView) viewGroup.getChildAt(i)).getDrawable());
                    if (drawable != null) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            drawable.setTint(color);
                        }
                    }
                }
                if (viewGroup.getChildAt(i) instanceof ImageTextView) {
                    ((ImageTextView) viewGroup.getChildAt(i)).setTextColor(color);
                } else if (viewGroup.getChildAt(i) instanceof ViewGroup) {
                    resetTextColor(viewGroup.getChildAt(i));
                } else if (viewGroup.getChildAt(i) instanceof TextView) {
                    ((TextView) viewGroup.getChildAt(i)).setTextColor(color);
                }
            }
        }
    }

    public View getActionBarTool() {
        return actionBarLayout.getActionBarView();
    }

    public void setActionBarTipType(ACTIONBARTIP_TYPE actionbartip_type) {
    }

    public ImageTextView getRightView() {
        if (actionBarLayout != null) {
            return actionBarLayout.findViewById(R.id.it_actionbar_common_right);
        }
        return null;
    }

    public void setRightOnClickListener(View.OnClickListener onClickListener) {
        if (getRightView() != null) {
            getRightView().setOnClickListener(onClickListener);
        }
    }

    public ImageTextView getTitleView() {
        if (actionBarLayout != null) {
            return actionBarLayout.findViewById(R.id.it_actionbar_common_title);
        }
        return null;
    }

    public void setHeaderBackgroundColor(int color) {
        if (actionBarLayout != null && actionBarLayout.getActionBarView() != null) {
            actionBarLayout.getActionBarView().setBackgroundColor(color);
        }
    }

    public void setTitle(CharSequence string) {
        if (actionBarLayout != null && actionBarLayout.findViewById(R.id.it_actionbar_common_title) instanceof ImageTextView) {
            ((ImageTextView) actionBarLayout.findViewById(R.id.it_actionbar_common_title)).setText(TextUtils.isEmpty(string) ? "" : string.toString());
        }
    }

    public ImageTextView getLeftView() {
        if (actionBarLayout != null) {
            return actionBarLayout.findViewById(R.id.it_actionbar_common_left);
        }
        return null;
    }

    /*@Override
    public void setStateBarColorAuto(boolean b) {

    }*/
    public void setLeftOnClickListener(View.OnClickListener onClickListener) {
        if (actionBarLayout != null && actionBarLayout.findViewById(R.id.it_actionbar_common_left) != null) {
            actionBarLayout.findViewById(R.id.it_actionbar_common_left).setOnClickListener(onClickListener);
        }
    }

    public AppCompatImageView findViewById(int id) {
        if (actionBarLayout != null) {
            return actionBarLayout.findViewById(id);
        }
        return null;
    }

    @Override
    public void onRelease(Object self) {
        actionBarLayout = null;
    }
    public ActionBarTool.Builder actionBarbuilder;
    QuickPageDragFrameLayout quickPageDragFrameLayout;
    public View genarateView(Builder actionBarbuilder) {
        this.actionBarbuilder = actionBarbuilder;
        actionBarLayout = new ActionBarLayout2(actionBarbuilder);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            actionBarLayout.setId(View.generateViewId());
        }
        Object objPage = fragment!=null?fragment:activity;
        if(objPage instanceof QuickPage){
            if(((QuickPage)objPage).canDrag()){
                quickPageDragFrameLayout =  new QuickPageDragFrameLayout(actionBarbuilder.activity);
                quickPageDragFrameLayout.addView(actionBarLayout);
                quickPageDragFrameLayout.setContextObject(objPage);
                return quickPageDragFrameLayout;
            }
        }
        return actionBarLayout;
    }

    public int getThemeBackgroundColor() {
        int backgroundColor = Color.TRANSPARENT;
        if(actionBarbuilder!=null){
            if(actionBarbuilder.getContentBackgroundColor()==null) {
                Log.i(TAG,"getThemeBackgroundColor="+context);
                TypedArray array = getContext().getTheme().obtainStyledAttributes(new int[]{
                        android.R.attr.colorBackground,
                        android.R.attr.textColorPrimary,
                        android.R.attr.colorPrimary,
                        android.R.attr.colorPrimaryDark,
                        android.R.attr.colorAccent,
                });
                backgroundColor = array.getColor(0, 0x00FF00FF);
            }else {
                backgroundColor = actionBarbuilder.getContentBackgroundColor();
            }
        }
        return backgroundColor;
    }
    public enum PaddingWith {
        statusBar_actionBar,
        statusBar,
        actionBar,
        none
    }
    public static class Builder {
        public ACTIONBAR_TYPE actionbarType = ACTIONBAR_TYPE.NORMAL;
        public int statusHeight;//状态栏高度
        public int actionBarPaddingTop_Last = 0;
        public boolean mixStatusActionBar = true;//是否使用融合模式，即actionbar padding 状态栏高度,优先级高于hasStatusBar
        public boolean hasStatusBar = true;
        public boolean hasActionBar = true;
        public PaddingWith contentViewPaddingTop = PaddingWith.none;
        public View statusbarView;
        public View actionBarView;
        public View contentView;
        public final Activity activity;
        public Integer contentBackgroundColor;

        public Builder(Activity activity) {
            this.activity = activity;
            setActionbarType(actionbarType);
        }

        public Builder setActionbarType(ACTIONBAR_TYPE actionbarType) {
            this.actionbarType = actionbarType;
            switch (actionbarType) {
                case NORMAL:
                case ACTION_STACK:
                    //case ACTION_TRANSPARENT:
                    hasStatusBar = true;
                    hasActionBar = true;
                    break;
                case NO_ACTION_BAR:
                case ACTION_STACK_NO_ACTION:
                    hasStatusBar = true;
                    hasActionBar = false;
                    break;
                case NO_ACTION_BAR_NO_STATUS:
                    hasStatusBar = false;
                    hasActionBar = false;
                    break;
                case NO_STATUS:
                case ACTION_STACK_NO_STATUS:
                    hasStatusBar = false;
                    hasActionBar = true;
                    break;
            }
            return this;
        }

        /**
         * body背景色
         *
         * @param contentBackgroundColor
         * @return
         */
        public Builder setContentBackgroundColor(Integer contentBackgroundColor) {
            this.contentBackgroundColor = contentBackgroundColor;
            return this;
        }

        public Integer getContentBackgroundColor() {
            return contentBackgroundColor;
        }

        public Builder setStatusHeight(int statusHeight) {
            this.statusHeight = statusHeight;
            return this;
        }

        public Builder setActionBarView(View actionBarView) {
            if (actionBarView != null) {
                this.actionBarView = actionBarView;
                actionBarPaddingTop_Last = actionBarView.getPaddingTop();
            }
            return this;
        }

        public Builder setContentView(View contentView) {
            this.contentView = contentView;
            return this;
        }

        public Builder setHasStatusBar(boolean hasStatusBar) {
            this.hasStatusBar = hasStatusBar;
            return this;
        }

        public Builder setContentViewPaddingTop(PaddingWith contentViewPaddingTop) {
            this.contentViewPaddingTop = contentViewPaddingTop;
            return this;
        }

        public Builder setHasActionBar(boolean hasActionBar) {
            this.hasActionBar = hasActionBar;
            return this;
        }

        public Builder setMixStatusActionBar(boolean mixStatusActionBar) {
            this.mixStatusActionBar = mixStatusActionBar;
            return this;
        }

//        public ActionBarLayout creat() {
//            return new ActionBarLayout(this);
//        }
    }

}
